package com.china08.yxyapi.api.manage.tec.teaching.continuedu.teatrainlesson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLessonItem;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduTask;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduTaskLesson;
import com.china08.yxyapi.entity.mg.continuedu.YxyContinueduUserTask;
import com.china08.yxyapi.entity.mg.continuedu.YxyContinueduUserTaskLesson;
import com.china08.yxyapi.entity.mg.continuedu.YxyContinueduUserTaskLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonItemRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduTaskLessonRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduTaskRepository;
import com.china08.yxyapi.repository.mg.continuedu.YxyContinueduUserTaskLessonItemRepository;
import com.china08.yxyapi.repository.mg.continuedu.YxyContinueduUserTaskLessonRepository;
import com.china08.yxyapi.repository.mg.continuedu.YxyContinueduUserTaskRepository;
import com.china08.yxyapi.util.RabbitMqSendUtil;

/**
 * 
 * @annotation: 继续教育-师训课程-报名
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class YxyTeaTrainLessonEnrolController extends YxyApiBaseController {

    @Autowired
    private YxyContinueduUserTaskRepository yxyContinueduUserTaskRepository;

    @Autowired
    private YxyContinueduUserTaskLessonRepository yxyContinueduUserTaskLessonRepository;

    @Autowired
    private YxyContinueduTaskLessonRepository yxyContinueduTaskLessonRepository;

    @Autowired
    private YxyContinueduLessonItemRepository yxyContinueduLessonItemRepository;

    @Autowired
    private YxyContinueduUserTaskLessonItemRepository yxyContinueduUserTaskLessonItemRepository;

    @Autowired
    private YxyContinueduTaskRepository YxyContinueduTaskRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/continuedu/teatrainlesson/enrol/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        String taskId = reqModel.taskId;
        RespModel respModel = new RespModel();
        YxyContinueduTask yxyContinueduTask = YxyContinueduTaskRepository.findById(taskId);
        /*************************** 初始化用户 -任务 *********************/
        YxyContinueduUserTask userTask = new YxyContinueduUserTask();
        String id = userId + "_" + taskId;
        YxyContinueduUserTask yxyContinueduUserTask = yxyContinueduUserTaskRepository.findByUserIdAndTaskId(userId,
                taskId);
        Assert.isNull(yxyContinueduUserTask, "已经报名过了");
        userTask.setId(id);
        userTask.setProgress(0.0);
        userTask.setStatus(0);
        userTask.setTaskId(taskId);
        userTask.setUserId(userId);
        userTask.setBeginDate(yxyContinueduTask.getBeginDate());
        userTask.setEndDate(yxyContinueduTask.getEndDate());
        userTask.setName(yxyContinueduTask.getName());
        userTask.setScore(yxyContinueduTask.getScore());
        yxyContinueduUserTaskRepository.save(userTask);

        /***************** 初始化用户 - 任务 - 课程 ***************/

        // 查询本次任务对应的课程
        List<YxyContinueduTaskLesson> taskLessons = yxyContinueduTaskLessonRepository.findByTaskId(taskId);

        if (taskLessons != null && taskLessons.size() > 0) {
            List<YxyContinueduUserTaskLesson> userTaskLessonList = new ArrayList<YxyContinueduUserTaskLesson>();
            // 获取任务对应的课程ID
            List<String> idsList = new ArrayList<String>();
            for (YxyContinueduTaskLesson yxyContinueduTaskLesson : taskLessons) {
                // id,使用 userId_taseId_lessonId 拼接组成,唯一
                String userTaskLessonId = userId + "_" + taskId + "_" + yxyContinueduTaskLesson.getLessonId();

                YxyContinueduUserTaskLesson userTaskLesson = new YxyContinueduUserTaskLesson();

                userTaskLesson.setId(userTaskLessonId);
                userTaskLesson.setLessonId(yxyContinueduTaskLesson.getLessonId());
                userTaskLesson.setProgress(0.0);
                userTaskLesson.setTaskId(taskId);
                userTaskLesson.setUserId(userId);
                userTaskLesson.setLessonName(yxyContinueduTaskLesson.getLessonName());
                userTaskLessonList.add(userTaskLesson);
                idsList.add(yxyContinueduTaskLesson.getLessonId());

            }

            yxyContinueduUserTaskLessonRepository.save(userTaskLessonList);

            /***************** 初始化用户 - 任务 - 课程 - 课时 ***************/

            // 根据获取的课程ID查询对应的课时
            List<YxyContinueduLessonItem> lessonItems = yxyContinueduLessonItemRepository
                    .findByLessonIdInOrderByOrderNumAsc(idsList);
            List<YxyContinueduUserTaskLessonItem> iList = new ArrayList<YxyContinueduUserTaskLessonItem>();
            for (YxyContinueduLessonItem yxyContinueduLessonItem : lessonItems) {
                // id,使用 userId_taskId_lessonId_lessonItemId 拼接组成,唯一
                String userTaskLessonIdItemId = userId + "_" + taskId + "_" + yxyContinueduLessonItem.getLessonId()
                        + "_" + yxyContinueduLessonItem.getId();
                YxyContinueduUserTaskLessonItem iTaskLessonItem = new YxyContinueduUserTaskLessonItem();

                iTaskLessonItem.setId(userTaskLessonIdItemId);
                iTaskLessonItem.setLessonId(yxyContinueduLessonItem.getLessonId());
                iTaskLessonItem.setLessonItemId(yxyContinueduLessonItem.getId());
                iTaskLessonItem.setStatus(0);
                iTaskLessonItem.setTaskId(taskId);
                iTaskLessonItem.setUserId(userId);
                iTaskLessonItem.setCurrentView(0l);
                iTaskLessonItem.setTitle(yxyContinueduLessonItem.getTitle());
                iTaskLessonItem.setVideoId(yxyContinueduLessonItem.getVideoId());
                iTaskLessonItem.setOrderNum(yxyContinueduLessonItem.getOrderNum());
                iList.add(iTaskLessonItem);
            }

            yxyContinueduUserTaskLessonItemRepository.save(iList);

        }
        // 更新本校报名人数
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("schoolId", schoolId);
        map.put("taskId", taskId);
        RabbitMqSendUtil.enrolStatis(map);

        respModel.setMessage("报名成功");

        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String taskId;// 任务ID

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
